<?php
/**
 * 文件描述
 * Created on 2021/8/9 13:50
 * Create by LZH
 */

namespace App\HttpController\Api\Admin\Admin;

use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Http\Message\Status;
use EasySwoole\VerifyCode\Conf;

/**
 * 验证码操作
 * Created on 2021/8/9 13:54
 * Create by LZH
 */
class VerifyCode extends Controller
{
    public function index()
    {
        // 配置验证码
        $config = new Conf();
        $code = new \EasySwoole\VerifyCode\VerifyCode($config);

        // 生成验证码
        $drawCode = $code->DrawCode();

        // 获取生成的验证码内容字符串 string(4) "0rnh"
        // 可存储起来和用户输入的验证码比对
        $codeStr = $drawCode->getImageCode();

        // 设置响应文件内容类型
        $this->response()->withHeader('Content-Type', 'image/png');

        // 向客户端输出验证码图片
        $this->response()->write($drawCode->getImageByte());
    }

    public function getBase64()
    {
        // 配置验证码
        $config = new Conf();
        $code = new \EasySwoole\VerifyCode\VerifyCode($config);

        // 生成验证码
        $drawCode = $code->DrawCode();

        // 获取生成的验证码内容字符串 string(4) "0rnh"
        // 可存储起来和用户输入的验证码比对
        $codeStr = $drawCode->getImageCode();

        // 向客户端输出验证码的 base64 编码，前端可用来生成图片
        $this->response()->write($drawCode->getImageBase64());
    }
}
